import Vue from 'vue'
import VueRouter from 'vue-router'
import LoginView from "@/views/LoginView";
import BookDetailView from "@/views/BookDetailView";
import MainView from "@/views/MainView";
import SearchResultView from "@/views/SearchResultView";
import ManagerLoginView from "@/views/ManagerLoginView";
import AddressManageView from "@/views/AddressManageView";
import CartView from "@/views/CartView";
import ManageView from "@/views/ManageView";
import OrdersView from "@/views/OrdersView";
import ManageOrdersView from "@/views/manage/ManageOrdersView";
import ManageBookView from "@/views/manage/ManageBookView";
import ManageCategoriesView from "@/views/manage/ManageCategoriesView.vue";
import PerformanceView from "@/views/manage/PerformanceView.vue";

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'home',
    component: MainView
  },
  {
    path: '/about',
    name: 'about',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../views/AboutView.vue')
  },
  {
    path: '/login',
    name: 'login',
    component: LoginView,
    meta: {
      title: '登录'
    }
  },
  {
    path: '/detail/:id',
    name: 'detail',
    component: BookDetailView,
    props: true,
    meta: {
      title: '商品详情'
    }
  },
  {
    path: '/search/:keyword',
    component: SearchResultView,
    props: true,
    meta: {
      title: '搜索结果'
    }
  },
  {
    path: '/manager/login',
    component: ManagerLoginView,
    meta: {
      title: '后台登录'
    }
  },
  {
    path: '/address',
    component: AddressManageView,
    meta: {
      title: '地址管理'
    }
  },
  {
    path: '/cart',
    component: CartView,
    meta: {
      title: '购物车'
    }
  },
  {
    path: '/orders',
    component: OrdersView,
    meta: {
      title: '订单管理'
    }
  },
  {
    path: '/manager',
    component: ManageView,
    children: [
      {path: '', redirect: 'orders'},
      {path: 'performance', component: PerformanceView, meta: {title: '后台 - 业绩视图'}},
      {path: 'orders', component: ManageOrdersView, meta: {title: '后台 - 订单管理'}},
      {path: 'books', component: ManageBookView, meta: {title: '后台 - 商品管理'}},
      {path: 'categories', component: ManageCategoriesView, meta: {title: '后台 - 类别管理'}}
    ],
    meta: {
      title: '后台管理'
    }
  }
]

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  if (to.meta.title) {
      document.title = to.meta.title + " - E-SHOP"
  } else {
    document.title = "E-SHOP"
  }
  next()
})

export default router
